package com.mflex.WHSmartWhouseAd.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

import androidx.annotation.Nullable;

import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.adapter.StockTaskAdapter;
import com.mflex.WHSmartWhouseAd.model.WHCompCheckTaskInfo;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xui.widget.textview.supertextview.SuperButton;
import com.xuexiang.xutil.common.StringUtils;

import java.util.ArrayList;
import java.util.List;


public class StockTaskListActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    EditText stvTaskNo;

    Spinner SpStorageType;
    ListView lvTaskList;


    SuperButton buttonQuery;


    SuperButton saveClear;

    MiniLoadingDialog mMiniLoadingDialog;

    private List<WHCompCheckTaskInfo> taskList;

    private String userName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stock_task_list);

        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载中...");

        stvTaskNo=findViewById(R.id.stv_TaskNo);
        lvTaskList=findViewById(R.id.lvTaskList);
        buttonQuery=findViewById(R.id.button_query);
        saveClear=findViewById(R.id.button_clear);
        SpStorageType=findViewById(R.id.StvStorageType);

        List<String> list=new ArrayList<String>();
        list.add("");
        list.add("智能仓");
        list.add("非智能仓");
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);
        adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        SpStorageType.setAdapter(adapter);
        SpStorageType.setSelection(0);


        Intent intent = getIntent();
        userName = intent.getStringExtra("userName");
        stvTaskNo.setOnKeyListener(this);
        lvTaskList.setOnItemClickListener(this);


        buttonQuery.setOnClickListener(this);
        saveClear.setOnClickListener(this);
       // initData();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_clear:
                clearButtonMd(view);
                break;
            case R.id.button_query:
                saveButtonMd(view);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
            switch (view.getId()) {
                case R.id.stv_TaskNo:
                    initData();
                    break;
                default:
                    break;
            }
        }
        return false;
    }

    private void initData() {
        mMiniLoadingDialog.show();
        String a=SpStorageType.getSelectedItem().toString();
        if (StringUtils.isEmpty(stvTaskNo.getText().toString())) {
            MediaPlayer mp = MediaPlayer.create(StockTaskListActivity.this, R.raw.error_voice);mp.start();
            new MaterialDialog.Builder(StockTaskListActivity.this)
                    .iconRes(R.drawable.icon_warning)
                    .title(R.string.tip_warning)
                    .content("任务单未输入")
                    .positiveText(R.string.lab_submit)
                    .show();
            return;
        }
        if (StringUtils.isEmpty(a)) {
            MediaPlayer mp = MediaPlayer.create(StockTaskListActivity.this, R.raw.error_voice);mp.start();
            new MaterialDialog.Builder(StockTaskListActivity.this)
                    .iconRes(R.drawable.icon_warning)
                    .title(R.string.tip_warning)
                    .content("类型不能为空")
                    .positiveText(R.string.lab_submit)
                    .show();
            return;
        }

        XHttp.get("api/WHSmartWhousePDA/PDAGetInventoryTaskNos")
                .params("TaskNo", stvTaskNo.getText().toString().trim())
                .params("WHType", SpStorageType.getSelectedItem().toString().trim())
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new SimpleCallBack<List<WHCompCheckTaskInfo>>() {

                    @Override
                    public void onSuccess(List<WHCompCheckTaskInfo> response) {
                        mMiniLoadingDialog.dismiss();
                        taskList = response;
                        StockTaskAdapter stockTaskAdapter = new StockTaskAdapter(StockTaskListActivity.this, taskList);
                        lvTaskList.setAdapter(stockTaskAdapter);
                        stvTaskNo.requestFocus();
                        stvTaskNo.setText(" ");
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        taskList = new ArrayList<>();
                        StockTaskAdapter stockTaskAdapter = new StockTaskAdapter(StockTaskListActivity.this, taskList);
                        lvTaskList.setAdapter(stockTaskAdapter);
                        new MaterialDialog.Builder(StockTaskListActivity.this)
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(e.getMessage())
                                .positiveText(R.string.lab_submit)
                                .show();
                    }
                });
    }

    private void clearButtonMd(View v) {
        stvTaskNo.setText("");
        stvTaskNo.requestFocus();

        taskList = new ArrayList<>();
        StockTaskAdapter stockTaskAdapter = new StockTaskAdapter(StockTaskListActivity.this, taskList);
        lvTaskList.setAdapter(stockTaskAdapter);
    }

    private void saveButtonMd(View v) {
        initData();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(StockTaskListActivity.this, StockTaskListDetailActivity.class);
        intent.putExtra("Type", SpStorageType.getSelectedItem().toString().trim());
        intent.putExtra("taskNo", taskList.get(i).getTaskNo());
        intent.putExtra("ID", taskList.get(i).getID());
        intent.putExtra("userName", userName);
        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        initData();
    }
}